package com.qianfeng.yime;

import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.model.LatLng;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.yime.adapter.CityLocationListAdapter;
import com.qianfeng.yime.model.City;
import com.qianfeng.yime.util.CommonUtils;
import com.qianfeng.yime.util.CurrentSingleton;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * Created by IDEA
 * User : SL
 * on  2015/3/11.
 * 17:03
 */
public class CityChooseActivity
        extends BaseActivity implements AdapterView.OnItemClickListener {
//        implements BDLocationListener

    private Button btnCityLocation;
    private Button btnCityHot;

    private LinkedList<City> cities;
    private CityLocationListAdapter adapter;
    private City hotCity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_choose);

//        SDKInitializer.initialize(getApplicationContext());

        setTitle("选择城市");

        btnCityLocation = (Button) findViewById(R.id.btn_positioning_city);
        btnCityHot = (Button) findViewById(R.id.btn_hot_city);
        ListView listCity = (ListView) findViewById(R.id.list_city);

//        initLocation();

        cities = new LinkedList<City>();
        adapter = new CityLocationListAdapter(this, cities);
        listCity.setAdapter(adapter);
        hotCity = new City();

        initCities();

        btnCityLocation.setOnClickListener(this);
        btnCityHot.setOnClickListener(this);
        listCity.setOnItemClickListener(this);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int id = v.getId();
        CurrentSingleton singleton = CurrentSingleton.getInstance();
        switch (id){
            case R.id.btn_positioning_city:
                //TODO 点击定位城市，将定位城市作为当前城市
                //  尚未实现定位，无法实现效果
                break;
            case R.id.btn_hot_city:
                //TODO 点击热门城市，将热门城市作为当前城市
                singleton.setCurrentCity(hotCity);
                finish();
                break;
        }
    }

    /**
     * 初始化城市列表
     */
    private void initCities() {
        HttpUtils httpUtils = new HttpUtils();
        String url = Constants.CITY_LIST_URL;
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                System.out.println("result = " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    int code = jsonObject.getInt("code");
                    if (code == 200) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        JSONArray citiesArray = data.getJSONArray("citys");
                        LinkedList<City> list = new LinkedList<City>();
                        for (int i = 0; i < citiesArray.length(); i++) {
                            JSONObject cityObject = citiesArray.getJSONObject(i);
                            City city = new City();
                            city.parseJson(cityObject);
//                            city.parseName(CityChooseActivity.this);
                            list.add(city);
                        }
                        cities.addAll(list);
                        System.out.println("cities.size() = " + cities.size());
                        adapter.notifyDataSetChanged();
                        JSONArray hot = data.getJSONArray("hot");
                        JSONObject hotCityObject = hot.getJSONObject(0);
                        hotCity.parseJson(hotCityObject);
//                        hotCity.parseName(CityChooseActivity.this);
                        String name = hotCity.getName();
                        btnCityHot.setText(name);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                CommonUtils.toast(CityChooseActivity.this, "联网失败");
                System.out.println("msg = " + msg);
            }
        });
    }

    /**
     * ListView的item的点击事件
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        City city = cities.get(position);
        CurrentSingleton singleton = CurrentSingleton.getInstance();
        singleton.setCurrentCity(city);
        finish();
    }

//    /**
//     * 定位当前城市
//     */
//    private void initLocation() {
//        LocationClientOption clientOption = new LocationClientOption();
//
//        clientOption.setOpenGps(true);  //是否打开GPRS
//        clientOption.setAddrType("all");//返回的定位结果包含地址信息
//        clientOption.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
//        clientOption.setIsNeedAddress(true);
//
//        LocationClient locationClient = new LocationClient(getApplicationContext(), clientOption);
//
//
//        locationClient.registerLocationListener(this);
//
//    }

//    /**
//     * 处理定位的接口
//     *
//     * @param bdLocation
//     */
//    @Override
//    public void onReceiveLocation(BDLocation bdLocation) {
//
//        double latitude = bdLocation.getLatitude();
//        double longitude = bdLocation.getLongitude();
//        final LatLng latLng = new LatLng(latitude, longitude);
//
//        final City cityLocation = new City();
//
//        HttpUtils httpUtils = new HttpUtils();
//        httpUtils.send(HttpRequest.HttpMethod.GET, "", new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                String result = responseInfo.result;
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//                    int code = jsonObject.getInt("code");
//                    if (code == 200) {
//                        JSONObject data = jsonObject.getJSONObject("data");
//                        cityLocation.parseLocationJson(data);
//                        cityLocation.setLatLng(latLng);
//
//                        String name = data.getString("name");
//                        btnCityLocation.setText(name);
//
//                        CurrentSingleton singleton = CurrentSingleton.getInstance();
//                        singleton.setCity(cityLocation);
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//            }
//        });
//    }

}